/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.entity.WbMtGroupStaff;
import com.zhuoyue.elmt.service.WbMtGroupStaffService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.WbMtGroup;
import com.zhuoyue.elmt.service.WbMtGroupService;

import java.util.List;

/**
 * 维保小组Controller
 *
 * @author JiangYH
 * @version 2017-02-09
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/wbMtGroup")
public class WbMtGroupController extends BaseController {

    @Autowired
    private WbMtGroupService wbMtGroupService;

    @Autowired
    private WbMtGroupStaffService wbMtGroupStaffService;

    @ModelAttribute
    public WbMtGroup get(@RequestParam(required = false) String id) {
        WbMtGroup entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = wbMtGroupService.get(id);
        }
        if (entity == null) {
            entity = new WbMtGroup();
        }
        return entity;
    }

    @RequiresPermissions("elmt:wbMtGroup:view")
    @RequestMapping(value = {"list", ""})
    public String list(WbMtGroup wbMtGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
        wbMtGroup.setMaintenUnitCode(UserUtils.getUser().getCompany().getCode());
        Page<WbMtGroup> page = wbMtGroupService.findPage(new Page<WbMtGroup>(request, response), wbMtGroup);
        List<WbMtGroup> list = page.getList();
        for (WbMtGroup wbMtGroup1 : list) {
            WbMtGroupStaff qryWbMtGroupStaff = new WbMtGroupStaff();
            qryWbMtGroupStaff.setGroupId(wbMtGroup1.getId());
            wbMtGroup1.setStaffList(wbMtGroupStaffService.findList(qryWbMtGroupStaff));
        }
        model.addAttribute("page", page);
        return "zhuoyue/elmt/wbMtGroupList";
    }

    @RequiresPermissions("elmt:wbMtGroup:view")
    @RequestMapping(value = "form")
    public String form(WbMtGroup wbMtGroup, Model model) {
        model.addAttribute("wbMtGroup", wbMtGroup);
        return "zhuoyue/elmt/wbMtGroupForm";
    }

    @RequiresPermissions("elmt:wbMtGroup:edit")
    @RequestMapping(value = "save")
    public String save(WbMtGroup wbMtGroup, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, wbMtGroup)) {
            return form(wbMtGroup, model);
        }
        wbMtGroup.setMaintenUnitCode(UserUtils.getUser().getCompany().getCode());
        wbMtGroupService.save(wbMtGroup);
        addMessage(redirectAttributes, "保存维保小组成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/wbMtGroup/?repage";
    }

    @RequiresPermissions("elmt:wbMtGroup:edit")
    @RequestMapping(value = "delete")
    public String delete(WbMtGroup wbMtGroup, RedirectAttributes redirectAttributes) {
        wbMtGroupService.delete(wbMtGroup);
        addMessage(redirectAttributes, "删除维保小组成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/wbMtGroup/?repage";
    }

}